package com.sky.mapper;

import com.sky.entity.Dish;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    @Select("select count(*) from dish")
    long getCount();

    @Select(
            "select dish.id, dish.name, category_id, price, image, description, dish.status, dish.update_time, category.name as category_name  from dish" +
            " left join category  on dish.category_id = category.id"
    )
    List<DishVO> getList();

    @Insert("insert into dish(name,category_id,price,image,description,status)values (#{name},#{categoryId},#{price},#{image},#{description},#{status})")
    /*todo 这个注解可以实现返回id
    当加入了这个注解之后，我们调用add()时，id会被会写到po对象
     */
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void add(Dish dish);

    @Delete("delete from dish where id=#{id}")
    void deleteById(Long id);

    @Select("select * from dish where id=#{id}")
    Dish getById(Long id);

    @Update("update dish set category_id=#{categoryId},description=#{description},image=#{image},name=#{name},price=#{price} where id=#{id}")
    void update(Dish dish);

    @Update("update dish set status=#{status} where id=#{id}")
    void setStatus(Dish dish);

    @Select("select * from dish where category_id=#{categoryId}")
    List<Dish> getDishListByCategoryId(Long categoryId);
}
